﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kugar.UI.RazorUI.RazorEngine;

namespace Kugar.UI.RazorUI.Demo.Forms
{

    [RazorForView("Views/index.cshtml")]
    public class Index:CefRazorFormBase
    {
        public Index(string url) : base(url)
        {
            this.FormLoaded += Index_FormLoaded;
        }

        private async void Index_FormLoaded(object sender, EventArgs e)
        {
            Debug.WriteLine("Index_FormLoaded");

            SetCookie("test", "1111", null);
            
            //按ID查找控件
            var element =await FindElementByIDAsync("btnShowWindow");

            //读取控件值
            var value =await element.GetAsync("value");

            await element.ExecuteAsync("click");
            //if (!this.SourceForm.GlobalObject.ContainsKey("test2"))
            //{
            //    this.SourceForm.GlobalObject.AddObject("test2");
            //}

            //if (!this.EmbedJsObject.ContainsKey("test22"))
            //{
            //    this.EmbedJsObject.AddFunction("test22").Execute += Index_Execute;
            //}

            //this.SourceForm.GlobalObject.AddDynamicProperty("ssss");
        }

        //开放给JS调用的函数
        //只能返回元数据类型或者newtonsoft的json类型
        public string TestApi(string url, int id)
        {
            return $"{url}{id}";
        }

        //输出模板数据
        public override IResult Execute()
        {
            var model=new Model();

            model.A1 = "sdfsfdsf";

            

            return View(model);
        }
    }
}
